package com.sojson.service.pay.ali.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoAuthResponse;
import com.sojson.config.exception.TransException;
import com.sojson.dao.MemberDao;
import com.sojson.entity.po.Member;
import com.sojson.service.pay.ali.PayAliService;
import com.sojson.util.StringUtil;
import com.sojson.util.encryption.Base64Util;
import com.sojson.util.pay.ali.AliUtil;
import com.sojson.util.pay.ali.constant.AliConstant;
import com.sojson.util.servlet.ServletUtil;

/**
 * 支付宝支付Service实现类
 * 
 * @author liu
 * @date 2021-03-24
 */
@Service
public class PayAliServiceImpl implements PayAliService {

    @Resource
    private MemberDao memberDaoImpl;

    /**
     * 认证授权
     * 
     * @return
     * @throws Exception 
     */
    @Override
    public void login() throws Exception {
        AlipayUserInfoAuthResponse login = AliUtil.login();

        ServletUtil.outHtml(login.getBody());
    }

    /**
     * 获取用户信息
     * 
     * @return
     * @throws Exception 
     */
    @Override
    public int updateInfo() throws Exception {
        String statePam = ServletUtil.getParameter(AliConstant.FIELD_STATE);
        if (StringUtil.isBlankObject(statePam)) {
            throw new TransException();
        }
        String state = Base64Util.decryption(statePam);
        AlipaySystemOauthTokenResponse info = AliUtil.getInfo(state);

        Member member = new Member();
        member.setNo(state);
        member.setOpenidZfb(info.getUserId());
        return memberDaoImpl.updatePartialByNo(member);
    }

}